package Demo01;

public class Main {
}

class Solution {
    public static int longestValidParentheses(String s) {
        char[] c = s.toCharArray();
        int[] arr = new int[c.length];
        for (int i = 1; i < c.length; i++) {
            if (c[i] == '(') {
                arr[i] = 0;
            } else {
                // c[i]=')'
                if (c[i - 1] == '(') {
                    arr[i] = 2;
                    if (i - 2 >= 0 && c[i - 2] == ')') {
                        arr[i] += arr[i - 2];
                    }
                } else {
                    // c[i-1]==')'
                    int tmp = arr[i - 1];
                    if (i - tmp - 1 >= 0 && c[i - tmp - 1] == '(') {
                        arr[i] = 2 + tmp;
                        if (i - tmp - 2 >= 0 && c[i - tmp - 2] == ')') {
                            arr[i] += arr[i - tmp - 2];
                        }
                    }

                }

            }
        }
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(findSubstringInWraproundString("zaba"));
    }

    public static int findSubstringInWraproundString(String s) {
        //0 1 2 3 4 ...
        //a b c d
        //存放以'a'为结尾的连续字符串
        int[] dp = new int[26];
        //"zaba"
        //处理第一个
        char c = s.charAt(0);
        dp[c - 'a'] = 1;
        int len = 1;
        for (int i = 1; i < s.length(); i++) {
            int cur = s.charAt(i)-'a';
            int prev = s.charAt(i-1)-'a';
            if ((cur == 0 && prev == 25)
                    || (prev + 1 == cur)) {
                //比较原来的dp大还是现在的len大
                len++;
            }else {
                len = 1;
            }
            dp[cur] = Math.max(dp[cur], len);
        }
        int ans = 0;
        for (int i = 0; i < dp.length; i++) {
            ans += dp[i];
        }
        return ans;
    }
}